package com.ruoyi.rycc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.rycc.domain.CcCourse;
import com.ruoyi.rycc.domain.CcSubject;
import com.ruoyi.rycc.service.ICcCourseService;
import com.ruoyi.rycc.service.ICcSubjectCourseService;
import com.ruoyi.rycc.service.ICcSubjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 课程管理Controller
 *
 * @author antg
 * @date 2021-09-04
 */
@Controller
@RequestMapping("/course/course")
public class CcCourseController extends BaseController {
    private String prefix = "course/course";

    @Autowired
    private ICcCourseService ccCourseService;
    @Autowired
    private ICcSubjectService ccSubjectService;
    @Autowired
    private ICcSubjectCourseService ccSubjectCourseService;

    @RequiresPermissions("course:course:view")
    @GetMapping()
    public String course(Model model) {
        //查询出所有的专题放入model域中
        List<CcSubject> ccSubjects = ccSubjectService.selectCcSubjectList(null);
        model.addAttribute("subjectList", ccSubjects);
        return prefix + "/course";
    }

    /**
     * 查询课程管理列表
     */
    @RequiresPermissions("course:course:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CcCourse ccCourse) {
        startPage();
        List<CcCourse> list = ccCourseService.selectCcCourseList(ccCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
    @RequiresPermissions("course:course:export")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcCourse ccCourse) {
        List<CcCourse> list = ccCourseService.selectCcCourseList(ccCourse);
        ExcelUtil<CcCourse> util = new ExcelUtil<CcCourse>(CcCourse.class);
        return util.exportExcel(list, "课程管理数据");
    }

    /**
     * 新增课程管理
     */
    @GetMapping("/add")
    public String add(Model model) {
        //查询出所有的专题放入model域中
        List<CcSubject> ccSubjects = ccSubjectService.selectCcSubjectList(null);
        model.addAttribute("subjectList", ccSubjects);
        return prefix + "/add";
    }

    /**
     * 新增保存课程管理
     */
    @RequiresPermissions("course:course:add")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CcCourse ccCourse, @RequestParam(value = "courseCoverFile", required = false) MultipartFile courseCover, @RequestParam(value = "courseShufflingFile", required = false) MultipartFile courseShuffling, @RequestParam(value = "coursePcShufflingFile", required = false) MultipartFile coursePcShuffling) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        //封面
        if (courseCover != null) {
            String courseCoverPath = FileUploadUtils.upload(filePath, courseCover);
            ccCourse.setCourseCover(courseCoverPath);
        }
        //轮播图
        if (courseShuffling != null) {
            String courseShufflingPath = FileUploadUtils.upload(filePath, courseShuffling);
            ccCourse.setCourseShuffling(courseShufflingPath);
        }
        //pc端轮播图
        if (coursePcShuffling != null) {
            String coursePcShufflingPath = FileUploadUtils.upload(filePath, coursePcShuffling);
            ccCourse.setCoursePcShuffling(coursePcShufflingPath);
        }
        return toAjax(ccCourseService.insertCcCourse(ccCourse));
    }

    /**
     * 修改课程管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        //查询出所有的专题放入model域中
        List<CcSubject> ccSubjects = ccSubjectService.selectCcSubjectList(null);
        mmap.put("subjectList", ccSubjects);
        CcCourse ccCourse = ccCourseService.selectCcCourseById(id);
        mmap.put("ccCourse", ccCourse);
        //将选中的分类从关联表中查询出来
        List<Integer> subIds = ccSubjectCourseService.selectSubjectIdsByCourseId(id);
        mmap.put("subIds", subIds);
        return prefix + "/edit";
    }

    /**
     * 修改保存课程管理
     */
    @RequiresPermissions("course:course:edit")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CcCourse ccCourse, @RequestParam(value = "courseCoverFile", required = false) MultipartFile courseCover, @RequestParam(value = "courseShufflingFile", required = false) MultipartFile courseShuffling, @RequestParam(value = "coursePcShufflingFile", required = false) MultipartFile coursePcShuffling) throws IOException {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        //封面
        if (courseCover != null) {
            String courseCoverPath = FileUploadUtils.upload(filePath, courseCover);
            ccCourse.setCourseCover(courseCoverPath);
        }
        //轮播图
        if (courseShuffling != null) {
            String courseShufflingPath = FileUploadUtils.upload(filePath, courseShuffling);
            ccCourse.setCourseShuffling(courseShufflingPath);
        }
        //pc端轮播图
        if (coursePcShuffling != null) {
            String coursePcShufflingPath = FileUploadUtils.upload(filePath, coursePcShuffling);
            ccCourse.setCoursePcShuffling(coursePcShufflingPath);
        }
        return toAjax(ccCourseService.updateCcCourse(ccCourse));
    }

    /**
     * 删除课程管理
     */
    @RequiresPermissions("course:course:remove")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ccCourseService.deleteCcCourseByIds(ids));
    }
}
